//
//  LocationManageTableViewCell.m
//  MeterMart
//
//  Created by ENERGY on 2018/9/27.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "LocationManageTableViewCell.h"

@interface LocationManageTableViewCell ()
@property (nonatomic, strong)UIView * whiteView;
@property (nonatomic, strong)UILabel * nameLab;
@property (nonatomic, strong)UILabel * phoneLab;
@property (nonatomic, strong)UILabel * detailAddressLab;
@property (nonatomic, strong)UILabel * defaultLab;
@property (nonatomic, strong)UILabel * deleteLab;
@property (nonatomic, strong)UIButton * defaultBtn;
@property (nonatomic, strong)UIButton * deleteBtn;
@property (nonatomic, strong)UIButton * defaultCoverBtn;
@property (nonatomic, strong)UIButton * deleteCoverBtn;
@property (nonatomic, strong)NSString * receiveid;
@property (nonatomic, strong)NSString * addressID;//地址ID
@end

@implementation LocationManageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = getColor(mainColor);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self.contentView addSubview:self.whiteView];
        [self.whiteView addSubview:self.nameLab];
        [self.whiteView addSubview:self.phoneLab];
        [self.whiteView addSubview:self.detailAddressLab];
        [self.whiteView addSubview:self.defaultBtn];
        [self.whiteView addSubview:self.deleteBtn];
        [self.whiteView addSubview:self.defaultLab];
        [self.whiteView addSubview:self.deleteLab];
        [self.whiteView addSubview:self.defaultCoverBtn];
        [self.whiteView addSubview:self.deleteCoverBtn];
        __weak typeof (self)weakSelf = self;
        [self.whiteView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.contentView.mas_left).mas_offset(10/WIDTH_6_SCALE);
            make.right.equalTo(weakSelf.contentView.mas_right).mas_offset(-10/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.contentView.mas_top).mas_offset(10/WIDTH_6_SCALE);
            make.height.mas_offset(127/WIDTH_6_SCALE);
        }];
        [self.nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.whiteView.mas_left).mas_offset(10/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.whiteView.mas_top).mas_offset(16/WIDTH_6_SCALE);
        }];
        [self.phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.whiteView.mas_right).mas_offset(-10/WIDTH_6_SCALE);
            make.top.equalTo(weakSelf.nameLab);
        }];
        [self.detailAddressLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.nameLab.mas_left);
            make.top.equalTo(weakSelf.nameLab.mas_bottom).mas_offset(12/WIDTH_6_SCALE);
        }];
        [self.defaultBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(weakSelf.whiteView.mas_bottom).mas_offset(-13/WIDTH_6_SCALE);
            make.left.equalTo(weakSelf.nameLab.mas_left);
            make.width.height.mas_offset(16/WIDTH_6_SCALE);
        }];
        [self.defaultLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(weakSelf.defaultBtn.mas_centerY);
            make.left.equalTo(weakSelf.defaultBtn.mas_right).mas_offset(6/WIDTH_6_SCALE);
        }];
        [self.deleteLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.whiteView.mas_right).mas_offset(-10/WIDTH_6_SCALE);
            make.centerY.equalTo(weakSelf.defaultLab.mas_centerY);
        }];
        [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.deleteLab.mas_left).mas_offset(-6/WIDTH_6_SCALE);
            make.centerY.equalTo(weakSelf.defaultLab.mas_centerY);
            make.width.mas_offset(14.5/WIDTH_6_SCALE);
            make.height.mas_offset(16/WIDTH_6_SCALE);
        }];
        [self.defaultCoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.defaultBtn.mas_top).mas_offset(-5/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.whiteView.mas_bottom);
            make.left.equalTo(weakSelf.defaultBtn.mas_left);
            make.right.equalTo(weakSelf.defaultLab.mas_right);
        }];
        [self.deleteCoverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(weakSelf.deleteBtn);
            make.right.equalTo(weakSelf.deleteLab.mas_right);
        }];
    }return self;
}
#pragma mark - init
- (UIView *)whiteView{
    if (!_whiteView) {
        _whiteView = [[UIView alloc] init];
        _whiteView.backgroundColor = [UIColor whiteColor];
    }return _whiteView;
}
-  (UILabel *)nameLab{
    if (!_nameLab) {
        _nameLab = [[UILabel alloc] init];
        _nameLab.font = DEF_FontSize_15;
        _nameLab.textColor = [UIColor blackColor];
        _nameLab.textAlignment = NSTextAlignmentLeft;
        _nameLab.text = @"用户名";
    }return _nameLab;
}
- (UILabel *)phoneLab{
    if (!_phoneLab) {
        _phoneLab = [[UILabel alloc] init];
        _phoneLab.font = DEF_FontSize_15;
        _phoneLab.textColor = [UIColor blackColor];
        _phoneLab.textAlignment = NSTextAlignmentRight;
        _phoneLab.text = @"1**********";
    }return _phoneLab;
}
- (UILabel *)detailAddressLab{
    if (!_detailAddressLab) {
        _detailAddressLab = [[UILabel alloc] init];
        _detailAddressLab.font = DEF_FontSize_12;
        _detailAddressLab.textColor = getColor(textGrayColor);
        _detailAddressLab.textAlignment = NSTextAlignmentLeft;
        _detailAddressLab.text = @"详细地址";
        _detailAddressLab.lineBreakMode = 0;
        _detailAddressLab.numberOfLines = 0;
    }return _detailAddressLab;
}
- (UILabel *)defaultLab{
    if (!_defaultLab) {
        _defaultLab = [[UILabel alloc] init];
        _defaultLab.font = DEF_FontSize_12;
        _defaultLab.textColor = [UIColor blackColor];
        _defaultLab.textAlignment = NSTextAlignmentLeft;
        _defaultLab.text = @"默认地址";
    }return _defaultLab;
}
- (UILabel *)deleteLab{
    if (!_deleteLab) {
        _deleteLab = [[UILabel alloc] init];
        _deleteLab.font = DEF_FontSize_12;
        _deleteLab.textColor = [UIColor blackColor];
        _deleteLab.textAlignment = NSTextAlignmentLeft;
        _deleteLab.text = @"删除";
    }return _deleteLab;
}
- (UIButton *)defaultBtn{
    if (!_defaultBtn) {
        _defaultBtn = [[UIButton alloc] init];
        [_defaultBtn setImage:[UIImage imageNamed:@"payUnselect"] forState:UIControlStateNormal];
    }return _defaultBtn;
}
- (UIButton *)deleteBtn{
    if (!_deleteBtn) {
        _deleteBtn = [[UIButton alloc] init];
        [_deleteBtn setImage:[UIImage imageNamed:@"locationDelete"] forState:UIControlStateNormal];
    }return _deleteBtn;
}
- (UIButton *)defaultCoverBtn{
    if (!_defaultCoverBtn) {
        _defaultCoverBtn = [[UIButton alloc] init];
        [_defaultCoverBtn addTarget:self action:@selector(defaultBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_defaultCoverBtn setBackgroundColor:[UIColor clearColor]];
    }return _defaultCoverBtn;
}
- (UIButton *)deleteCoverBtn{
    if (!_deleteCoverBtn) {
        _deleteCoverBtn = [[UIButton alloc] init];
        [_deleteCoverBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_deleteCoverBtn setBackgroundColor:[UIColor clearColor]];
    }return _deleteCoverBtn;
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.phoneLab.text = model[@"receivephone"];
    self.detailAddressLab.text = [NSString stringWithFormat:@"%@%@%@  %@", model[@"receiveprovince"], model[@"receivecity"], model[@"receivearea"], model[@"receiveaddress"]];
    self.nameLab.text = model[@"receivename"];
    self.receiveid = model[@"receiveid"];
    if ([model[@"status"] isEqualToString:@"1"]) {
        [self.defaultBtn setImage:[UIImage imageNamed:@"defaultLocation"] forState:UIControlStateNormal];
    }else{
        [self.defaultBtn setImage:[UIImage imageNamed:@"payUnselect"] forState:UIControlStateNormal];
    }
    self.addressID = model[@"receiveid"];
}

#pragma mark - action
- (void)deleteBtnClick:(UIButton *)sender{
    NSLog(@"删除");
    if (self.deleteBtnBlock) {
        self.deleteBtnBlock(self.addressID);
    }
}
- (void)defaultBtnClick:(UIButton *)sender{
    if (self.defaultBtnSetBlock) {
        self.defaultBtnSetBlock(self.addressID);
    }
}
@end
